<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/9/28
 * Time: 20:34
 */
class Express_model extends CI_Model
{
    private $table  = 'express';

    public function page_list($page_num, $num, $params)
    {
        $this->db->select('id,name,company,create_at');
        $this->db->from($this->table);

        if(!empty($params['keyWord'])){
            $this->db->like('name', $params['keyWord']);
            $this->db->or_like('company', $params['keyWord']);
        }

        $this->db->order_by('id', 'ASC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

    public function total($params)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        if(!empty($params['keyWord'])){
            $this->db->like('name', $params['keyWord']);
            $this->db->or_like('company', $params['keyWord']);
        }
        return $this->db->count_all_results();
    }

    public function insert($postData)
    {
        $data = array(
            'name'      => $postData['name'],
            'company'   => $postData['company'],
            'create_at' => date('Y-m-d H:i:s'),
            'update_at' => '0000-00-00 00:00:00'
        );

        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }



    public function update($postData)
    {
        $data = array(
            'name'      => $postData['name'],
            'company'   => $postData['company'],
            'update_at' => date('Y-m-d H:i:s')
        );

        $this->db->where('id', $postData['id']);
        return $this->db->update($this->table, $data);
    }

    public function delete($id)
    {
        $this->db->where('id', $id);
        return $this->db->delete($this->table);
    }

    public function findById($id)
    {
        $this->db->select('id,name,company');
        $this->db->from($this->table);
        $this->db->where('id', $id);

        $this->db->limit(1);
        return $this->db->get();
    }

    public function findAll()
    {
        $this->db->select('id,name,company');
        $this->db->from($this->table);

        return $this->db->get();
    }

    public function findByName($name)
    {
        $this->db->select('id,name,company');
        $this->db->from($this->table);
        $this->db->where('company', $name);
        $this->db->limit(1);
        return $this->db->get();
    }
    
    public function isExist($name, $company)
    {
        $this->db->select('id,name,company');
        $this->db->from($this->table);
        $this->db->where('name', $name);
        $this->db->or_where('company', $company);

        return $this->db->get();
    }
    
}
